/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.dao;

import java.util.List;
import java.util.ArrayList;

import org.hibernate.Query;
import org.hibernate.Session;

import net.gis.eemc.domain.HibernateUtil;
import net.gis.eemc.domain.Discipline;
import net.gis.eemc.domain.ExcerptGos;
import net.gis.eemc.domain.GuidelinesCp;
import net.gis.eemc.domain.GuidelinesLab;
import net.gis.eemc.domain.LectureSynopsis;
import net.gis.eemc.domain.WorkProgram;

/**
 *
 * @author Sergey
 */
public class DisciplineDocsDAOImpl implements DisciplineDocsDAO {

    Session session = null;

    // ExcerptGOS
    @Override
    public void addExcerptGOS(ExcerptGos doc) {
        add(doc);
    }

    @Override
    public void updateExcerptGOS(ExcerptGos doc) {
        update(doc);
    }

    @Override
    public void removeExcerptGOS(ExcerptGos doc) {
        remove(doc);
    }

    @Override
    public List<ExcerptGos> listExcerptGos() {
        return (List<ExcerptGos>) getDocs("from ExcerptGos as excerptGos");
    }

    @Override
    public List<ExcerptGos> listExcerptGos(Discipline discipline) {
        return (List<ExcerptGos>) getDocs("from ExcerptGos as excerptGos where excerptGos.discipline.id=\"" + discipline.getId().toString() + "\"");
    }

    // GuidlinesCp
    @Override
    public void addGuidlinesCp(GuidelinesCp doc) {
        add(doc);
    }

    @Override
    public void updateGuidlinesCp(GuidelinesCp doc) {
        update(doc);
    }

    @Override
    public void removeGuidlinesCp(GuidelinesCp doc) {
        remove(doc);
    }

    @Override
    public List<GuidelinesCp> listGuidelinesCp() {
        return (List<GuidelinesCp>) getDocs("from GuidelinesCp as guidelinesCp");
    }

    @Override
    public List<GuidelinesCp> listGuidelinesCp(Discipline discipline) {
        return (List<GuidelinesCp>) getDocs("from GuidelinesCp as guidelinesCp where guidelinesCp.discipline.id=\"" + discipline.getId().toString() + "\"");
    }

    // GuidlinesLab
    @Override
    public void addGuidlinesLab(GuidelinesLab doc) {
        add(doc);
    }

    @Override
    public void updateGuidlinesLab(GuidelinesLab doc) {
        update(doc);
    }

    @Override
    public void removeGuidlinesLab(GuidelinesLab doc) {
        remove(doc);
    }

    @Override
    public List<GuidelinesLab> listGuidelinesLab() {
        return (List<GuidelinesLab>) getDocs("from GuidelinesLab as guidelinesLab");
    }

    @Override
    public List<GuidelinesLab> listGuidelinesLab(Discipline discipline) {
        return (List<GuidelinesLab>) getDocs("from GuidelinesLab as guidelinesLab where guidelinesLab.discipline.id=\"" + discipline.getId().toString() + "\"");
    }

    // LectureSynopsis
    @Override
    public void addLectureSynopsis(LectureSynopsis doc) {
        add(doc);
    }

    @Override
    public void updateLectureSynopsis(LectureSynopsis doc) {
        update(doc);
    }

    @Override
    public void removeLectureSynopsis(LectureSynopsis doc) {
        remove(doc);
    }

    @Override
    public List<LectureSynopsis> listLectureSynopsis() {
        return (List<LectureSynopsis>) getDocs("from LectureSynopsis as lectureSynopsis");
    }

    @Override
    public List<LectureSynopsis> listLectureSynopsis(Discipline discipline) {
        return (List<LectureSynopsis>) getDocs("from LectureSynopsis as lectureSynopsis where lectureSynopsis.discipline.id=\"" + discipline.getId().toString() + "\"");
    }

    // WorkProgram
    @Override
    public void addWorkProgram(WorkProgram doc) {
        add(doc);
    }

    @Override
    public void updateWorkProgram(WorkProgram doc) {
        update(doc);
    }

    @Override
    public void removeWorkProgram(WorkProgram doc) {
        remove(doc);
    }

    @Override
    public List<WorkProgram> listWorkProgram() {
        return (List<WorkProgram>) getDocs("from WorkProgram as workProgram");
    }

    @Override
    public List<WorkProgram> listWorkProgram(Discipline discipline) {
        return (List<WorkProgram>) getDocs("from WorkProgram as workProgram where workProgram.discipline.id=\"" + discipline.getId().toString() + "\"");
    }

    ///////////////////////////////////////////////////////////////////////////
    // Private methods
    ///////////////////////////////////////////////////////////////////////////
    private void add(Object obj) {
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            session.save(obj);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    private void update(Object obj) {
        org.hibernate.Transaction tx = null;
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            session.update(obj);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    private void remove(Object obj) {
        org.hibernate.Transaction tx = null;
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            session.delete(obj);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
    }

    private List getDocs(String query) {
        List docs = null;
        org.hibernate.Transaction tx = null;

        try {
            this.session = HibernateUtil.getSessionFactory().getCurrentSession();

            tx = session.beginTransaction();
            Query q = session.createQuery(query);
            docs = q.list();
            tx.commit();
        } catch (Exception e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        }
        return docs;
    }
}
